package com.kmxd.ams.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kmxd.ams.client.system.ISysDeptSvc;
import com.kmxd.ams.client.system.dto.SysDeptAddDTO;
import com.kmxd.ams.client.system.dto.SysDeptEditDTO;
import com.kmxd.ams.client.system.dto.SysDeptQueryDTO;
import com.kmxd.ams.client.system.vo.SysDeptListVO;
import com.kmxd.ams.client.system.vo.SysDeptVO;
import com.kmxd.ams.core.common.enums.LogLevelEnum;
import com.kmxd.ams.core.common.enums.LogTypeEnum;
import com.kmxd.ams.core.log.annotation.AutoLog;
import com.kmxd.ams.core.result.R;
import com.kmxd.ams.core.util.SecurityUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 部门表 前端控制器
 *
 * @author admin
 * @since 2021-06-10
 */
@Tag(name = "部门")
@RestController
@RequestMapping("/system/sysDept")
@AllArgsConstructor
public class SysDeptCtl {

  private ISysDeptSvc sysDeptSvc;

  /** 分页查询 */
  @GetMapping("/selectPage")
  @AutoLog(module = "部门管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "分页查询")
  public R<Page<SysDeptListVO>> selectPage(Page reqPage, SysDeptQueryDTO req) {
    req.setComId(ObjectUtil.isNotEmpty(req.getComId()) ? req.getComId() : SecurityUtil.getCropId());
    return R.success(sysDeptSvc.selectPage(reqPage, req));
  }

  /** 列表查询 */
  @GetMapping("/selectList")
  @AutoLog(module = "部门管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "列表查询")
  public R<List<SysDeptListVO>> selectList(SysDeptQueryDTO req) {
    req.setComId(ObjectUtil.isNotEmpty(req.getComId()) ? req.getComId() : SecurityUtil.getCropId());
    return R.success(sysDeptSvc.selectList(req));
  }

  /** 新增 */
  @PostMapping("/add")
  @SaCheckPermission("system:sysDept:add")
  @AutoLog(module = "部门管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "新增")
  public R<Void> add(@Validated @RequestBody SysDeptAddDTO req) {
    sysDeptSvc.add(req);
    return R.success();
  }

  /** 修改 */
  @PostMapping("/edit")
  @SaCheckPermission("system:sysDept:edit")
  @AutoLog(module = "部门管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "修改")
  public R<Void> edit(@Validated @RequestBody SysDeptEditDTO req) {
    sysDeptSvc.edit(req);
    return R.success();
  }

  /** 删除 */
  @PostMapping("/remove")
  @SaCheckPermission("system:sysDept:remove")
  @AutoLog(module = "部门管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.HIGH, title = "删除")
  public R<Void> remove(@RequestParam String ids) {
    sysDeptSvc.remove(ids);
    return R.success();
  }

  /** 查看 */
  @GetMapping("/view")
  @AutoLog(module = "部门管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查看")
  public R<SysDeptVO> view(@RequestParam Long id) {
    return R.success(sysDeptSvc.view(id));
  }

  /** 查询部门树 */
  @GetMapping("/listDeptTree")
  @AutoLog(module = "部门管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询部门树")
  public R<List<Tree<String>>> listDeptTree(
      @RequestParam(value = "cropId", required = false) Long cropId) {
    return R.success(
        sysDeptSvc.listDeptTree(ObjectUtil.isNotEmpty(cropId) ? cropId : SecurityUtil.getCropId()));
  }

  /** 查询下拉部门树，排除当前节点以及子节点 */
  @GetMapping("/selectDeptTreeAndExcludeNode")
  @AutoLog(
      module = "部门管理",
      type = LogTypeEnum.SYSTEM,
      level = LogLevelEnum.LOW,
      title = "查询下拉部门树，排除当前节点以及子节点")
  public R<List<Tree<String>>> selectTreeAndExcludeNode(
      @RequestParam(required = false) String nodeId) {
    return R.success(sysDeptSvc.selectDeptTreeAndExcludeNode(nodeId));
  }

  /** 查询下拉部门树 */
  @GetMapping("/selectDeptTree")
  @AutoLog(module = "部门管理", type = LogTypeEnum.SYSTEM, level = LogLevelEnum.LOW, title = "查询下拉部门树")
  public R<List<Tree<String>>> selectTree() {
    return R.success(sysDeptSvc.selectDeptTree());
  }
}
